<?php
	include '../include/functions.php';
//set session save path
session_save_path($session_path);
// Initialize a session. 
session_start();
$count++;
$start = time();
$sessionId = session_id();

?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
  <title>JoshHughes.com NHL Hockey Pool</title>
	<link rel="shortcut icon" href="http://hockey.joshhughes.com/NHL/images/nhlFavicon.gif" />
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/NHL.css" type="text/css">
    <script src="http://hockey.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/hockey.js" type="text/javascript"></script>
    <style>
	.underlinecell {
		border-bottom: 2px solid #FF0000;
	}
	.overlinecell {
		border-top: 2px solid #FF0000;
	}
	</style>
</head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >
					
						<table cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
							<tr>
							<td class="bluebg">
						<?php
						dbConnect();
						//check to see if a Weeknum value was passed, if not, set WeekNum to CurrWeek.
						if (($Weeknum == NULL) || ($Weeknum == ""))
						{
							$Weeknum = getWeek("NHL");
								
							//check to make sure $Weeknum > 1 AND $Weeknum < 21
							$Weeknum = checkWeekBounds($Weeknum);
							
							if ((date("w")==2)||(date("w")==3)) //If Tuesday or Wednesday, set Weeknum back one week to review scores.
								if ($Weeknum >= 2)
									$Weeknum--;
										
							if (($Weeknum > 17) && (date("w")==1)) //During Playoff time set Weeknum back one week to review scores on Monday also.
								$Weeknum--;
								
						}
						$Weeknum = 12;
						//Write SQL Query to extract specified data
						//$SQL = "SELECT * FROM NHLSchedule Where (WEEK=".$Weeknum.") AND (Winner != '-')";
						//Open Record Set using SQL Query
						//$result = mysql_query($SQL) or die("Cannot query the database.<br>");
						?>
						<table bgcolor="#FFFFFF" callpadding="5" cellspacing="0" width="700" align="center">
							<tr><td align="center"><H1>Series Results</H1></td></tr>
						</table>
						
						<table bgcolor="#FFFFFF" border="1" callpadding="5" cellspacing="0" width="700" align="center">
						<?
						$query="SELECT * FROM NHLSeries Where Team1 <> 'TBD' ORDER BY Week ASC";
						$doQuery=mysql_query($query);
						$numrows=mysql_num_rows($doQuery);
						$num = 1;
						if($numrows > 0)
						{
							while($results=mysql_fetch_array($doQuery))
							{
								//Fill Arrays With Data
								$homeTeams[$num]=$results['Team2'];
								$awayTeams[$num]=$results['Team1'];
								$winners[$num]=$results['Winner'];
								$GameNums[$num]=$results['NumGames'];
								$num++;
							}
							//Print Series Number Line
							echo "<tr bgcolor=\"#dddddd\"><td width=\"100\"><b>Series:</b></td>";
							for ($s = 1; $s < $num; $s++)
								echo "<td width=\"40\" align=\"center\">$s</td>";
							echo "<td>&nbsp;</td></tr>";
							
							//Print Away Line
							echo "<tr bgcolor=\"#FFFFFF\"><td><b>Team1</b></td>";
							foreach($awayTeams as $team)
								echo "<td width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$team.".gif\" alt=\"".$team."\"  title=\"".$team."\" border=\"0\" /></td>";
							echo "<td rowspan=\"5\" valign=\"center\" align=\"center\"><b>Series<br/>Score</b></td>";
							echo "</tr>";
							
							//Print @ Line
							echo "<tr bgcolor=\"#FFFFFF\"><td>&nbsp;</td>";
							foreach($homeTeams as $team)
							  echo "<td width=\"40\" align=\"center\">@</td>";
							echo "</tr>";
							
							//Print Home Line
							echo "<tr bgcolor=\"#FFFFFF\" bordercolor=\"#172693\"><td class=\"underlinecell\"><b>Team2</b></td>";
							foreach($homeTeams as $team)
							  echo "<td class=\"underlinecell\" width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$team.".gif\" alt=\"".$team."\"  title=\"".$team."\" border=\"0\" /></td>";
							echo "</tr>";
							
							$cols = $numrows + 3;
							
							//Print Winner Line
							echo "<tr bgcolor=\"#FFFFFF\"><td><span style=\"color: rgb(255, 0, 0); font-weight: bold;\">Winner</span></td>";
							foreach($winners as $team)
								if($team != "-")
									echo "<td width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$team.".gif\" alt=\"".$team."\"  title=\"".$team."\" border=\"0\" /></td>";
								else
									echo "<td width=\"40\" align=\"center\">&nbsp;</td>";
							echo "</tr>";
							//Print Number of Games Line
							echo "<tr bgcolor=\"#FFFFFF\"><td class=\"underlinecell\"># of games</td>";
							foreach($GameNums as $num_games)
								if($num_games != "-")
									echo "<td class=\"underlinecell\" width=\"40\" align=\"center\"><span style=\"color: #FF0000; font-weight: bold;\">$num_games</span></td>";
								else
									echo "<td width=\"40\" align=\"center\">&nbsp;</td>";
							echo "</tr>";
							
							//Print Rows For User & Picks
							$numCorrectPicks = array_fill(1,$numrows, 0);
							$SeriesnumCorrectPicks = array_fill(1,$numrows, 0);
							$IDs[] = 0;
							$WeeklyScores[] = 0;
							$breakdownString[] = "";
							$totalScore = 0;						
							$totalGuess = 0;
							//Get Data From Users Who Picked
							$UserPickedSQL = "SELECT DISTINCT UserID FROM NHLSeriesPicks WHERE Team IS NOT NULL ORDER BY UserID ASC";
							$UserPickedresult = mysql_query($UserPickedSQL) or die("Cannot query the database for if user picked.<br>");
							$row = 0;
							do{
								if ($myrow["UserID"] != NULL)
								{						
									$breakdownString[$row] = "<td>".makeUserLink($myrow["UserID"])."</td>\n";
									$WeeklyScores[$row] = 0;
									$SeriesnumCorrectPicks[$row] = 0;
									$IDs[$row] = $myrow["UserID"];
									for($j = 1; $j <= $numrows; $j++)
									{	
										$insideSQL = "SELECT Team FROM NHLSeriesPicks WHERE UserID = ".$myrow["UserID"]." AND Week = $j AND Team IS NOT NULL ORDER BY UserID ASC";
										$insidePickedresult = mysql_query($insideSQL) or die("Cannot query the database for user picks.<br>");
										
										$insiderow = mysql_fetch_array($insidePickedresult);
										$pick = $insiderow["Team"];
						
										if($winners[$j] != "-")
										{
											if($pick == $winners[$j])
											{
												$breakdownString[$row].= "<td width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$pick.".gif\" alt=\"".$pick."\" title=\"".$pick."\" border=\"0\" /></td>\n";
												//$WeeklyScores[$row]++;
												switch(true)
												{
													case ($j < 9):
														$WeeklyScores[$row]+=5;
														break;
													case (($j >= 9) && ($j < 13)):
														$WeeklyScores[$row]+=8;
														break;
													case (($j >= 13) && ($j < 15)):
														$WeeklyScores[$row]+=12;
														break;
													case ($j == 15):
														$WeeklyScores[$row]+=15;
														break;
												}
												$numCorrectPicks[$j]++;
											}
											else
											{	
												if($pick == "")
													$breakdownString[$row].= "<td width=\"40\" align=\"center\">&nbsp;</td>\n";
												else
													$breakdownString[$row].= "<td width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/images/red-x.gif\" /></td>\n";
											}
										}
										else
										{
											$InProgressResult = mysql_query("SELECT Started FROM NHLSeries WHERE Week=".$Weeknum) or die("Cannot query the database for in progress results.<br>");
											$ProgressRow = mysql_fetch_array($InProgressResult);
											if($ProgressRow["Started"] == 1)
												$breakdownString[$row].= "<td width=\"40\" align=\"center\"><img src=\"http://hockey.joshhughes.com/NHL/images/small/".$pick.".gif\" alt=\"".$pick."\" title=\"".$pick."\" border=\"0\" /></td>\n";
											else
												$breakdownString[$row].= "<td width=\"40\" align=\"center\">&nbsp;</td>\n";
										}
										
									}
									$breakdownString[$row].= "<td align=\"center\">$WeeklyScores[$row]</td>\n";

									$breakdownString[$row].= "</tr>\n<tr bgcolor=\"#FFFFFF\"><td width=\"40\" align=\"right\">(Game Guess)</td>\n";
									$totalScore += $WeeklyScores[$row];
									
									//Show user's pick for Number of game the best-of-seven series will last..
									for($j = 1; $j <= $numrows; $j++)
									{	
										$insideSQL = "SELECT NumGames FROM NHLSeriesPicks WHERE UserID = ".$myrow["UserID"]." AND Week = $j AND Team IS NOT NULL ORDER BY UserID ASC";
										$insidePickedresult = mysql_query($insideSQL) or die("Cannot query the database for user picks.<br>");
										
										$insiderow = mysql_fetch_array($insidePickedresult);
										$pick = $insiderow["NumGames"];
						
										if($GameNums[$j] != 0)
										{
											if($pick == $GameNums[$j])
											{
												$breakdownString[$row].= "<td width=\"40\" align=\"center\"><span style=\"color: #FF0000; font-weight: bold;\">$pick</span></td>\n";
												switch(true)
												{
													case ($j < 13):
														$SeriesnumCorrectPicks[$row]+=2;
														break;
													case (($j >= 13) && ($j < 15)):
														$SeriesnumCorrectPicks[$row]+=3;
														break;
													case ($j == 15):
														$SeriesnumCorrectPicks[$row]+=5;
														break;
												}
											}
											else
											{	
												if($pick == "")
													$breakdownString[$row].= "<td width=\"40\" align=\"center\">&nbsp;</td>\n";
												else
													$breakdownString[$row].= "<td width=\"40\" align=\"center\">$pick</td>\n";
											}
										}
										else
										{					
											$breakdownString[$row].= "<td width=\"40\" align=\"center\">&nbsp;</td>\n";
										}
										
									}
									$breakdownString[$row].= "<td align=\"center\">$SeriesnumCorrectPicks[$row]</td></tr>\n";
									$breakdownString[$row].= "<tr><td align=\"right\" colspan=\"".($cols-2)."\"><b>Total Score</b></td><td align=\"center\"><span style=\"color: rgb(255, 0, 0); font-weight: bold; font-size: 16px;\">".($WeeklyScores[$row]+$SeriesnumCorrectPicks[$row])."</span></td></tr>\n";
									$row++;
									
								}
								
							}while($myrow = mysql_fetch_array($UserPickedresult));
							
							// Sort by weekly score
							for($i=sizeof($WeeklyScores); $i>0; $i--)
							{
								for($i2=1; $i2 <= $i; $i2++)
								{
									if(($WeeklyScores[$i2-1]+$SeriesnumCorrectPicks[$i2-1]) < ($WeeklyScores[$i2]+$SeriesnumCorrectPicks[$i2]))
									{
										swapthese($WeeklyScores[$i2-1],$WeeklyScores[$i2]);
										swapthese($breakdownString[$i2-1],$breakdownString[$i2]);
										swapthese($SeriesnumCorrectPicks[$i2-1],$SeriesnumCorrectPicks[$i2]);
										swapthese($IDs[$i2-1],$IDs[$i2]);
									};
									
								}
							}
							// PRint the lines
							for ($num = 0; $num <= sizeof($breakdownString); $num++)
							{
								if ($userID)
								{
									if ($userID == $IDs[$num])
										echo "<tr bgcolor=\"#ffff33\">".$breakdownString[$num];
									else
										echo "<tr bgcolor=\"#dddddd\">".$breakdownString[$num];
								}
								else
								{
									echo "<tr  bgcolor=\"#dddddd\">".$breakdownString[$num];
								}
							}
							
							// Print last rows of table
							echo "<tr bgcolor=\"".getRowColor($row)."\">";
							echo "<td class=\"overlinecell\" align=\"right\"><b>Total</b><br/>($row users)</td>\n";
							for ($j = 1; $j <= $numrows; $j++)
								echo "<td class=\"overlinecell\" width=\"40\" align=\"center\">$numCorrectPicks[$j]</td>\n";
							echo "<td class=\"overlinecell\" align=\"center\"><b>Avg</b><br/>".round($totalScore/$row, 1)."</td>\n";
							//echo "<td class=\"overlinecell\" align=\"center\"><b>Avg</b><br/>".round($totalGuess/$row, 1)."</td>\n";
							echo "</tr>\n";
							
						}
						else
						{
							print '<tr bgcolor=\"#FFFFFF\"><td colspan=\"$cols\" align=\"center\">No Games Set for This Series Yet.</td></tr>';
						}
						//Close table
						echo "</table>";
						?>
						</td>
						<tr><td>&nbsp;</td></tr>
						<!--
						<tr><td>
						<div class="redbg" style="padding: 2px;">
							<table width="100%" cellpadding="0" cellspacing="0" border="0">
								<tr>
									<td align="left"><span class="whitelink">View Game Scores</span></td>
									<td align="right"><img src="http://hockey.joshhughes.com/images/showhide.gif" title="Show/Hide Scores" alt="Show/Hide Scores" border="0"  onclick="new Effect.toggle('scoresframe', 'blind');" /></td>
								</tr>
							</table><iframe style="display:none;" id="scoresframe" name="scoresframe" src="http://hockey.joshhughes.com/NHL/rss/<?= getSeasonYear("NHL"); ?>/week<?= $Weeknum; ?>.xml" height="250" width="100%" frameborder="0" scrolling="no" ></iframe>
						</div>
						</td></tr>
						-->
						</tr>
						</table><br/>
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("layout/footer.html"); ?>
			
		</div>
</div>
  </body>
</html>